#include "Bullet.h"

IMAGE* Bullet::imgBullets[4] = { NULL };			// 初始化子弹图片

// 构造函数
Bullet::Bullet()
{
	// 加载子弹图片
	if (imgBullets[0] == NULL)
	{
		IMAGE imgTmp;
		loadimage(&imgTmp, "./resources/image/bullet.png", BULLET_SIZE * 4, BULLET_SIZE);
		SetWorkingImage(&imgTmp);				// 设定当前绘图目标为imgTmp
		for (int i = 0; i < 4; i++)
		{
			imgBullets[i] = new IMAGE;
			getimage(imgBullets[i], i * BULLET_SIZE, 0, BULLET_SIZE, BULLET_SIZE);
		}
		SetWorkingImage();						// 设置绘图目标为绘图窗口
	}
}

// 子弹绘制
void Bullet::draw()
{
	myDrawImage(this->x, this->y, imgBullets[this->direction], GAME_WIDTH, GAME_HEIGHT);
}

// 子弹运行
void Bullet::run()
{
	switch (this->direction)
	{
	case TANK_DIRECTION_UP:
		if (this->y - this->speed >= 0)
		{
			this->y -= this->speed;
		}
		else
		{
			this->status = BULLET_STATUS_FREE;
		}
		break;
	case TANK_DIRECTION_DOWN:
		if (this->y + BULLET_SIZE + this->speed <= GAME_HEIGHT)
		{
			this->y += this->speed;
		}
		else
		{
			this->status = BULLET_STATUS_FREE;
		}
		break;
	case TANK_DIRECTION_LEFT:
		if (this->x >= this->speed)
		{
			this->x -= this->speed;
		}
		else
		{
			this->status = BULLET_STATUS_FREE;
		}
		break;
	case TANK_DIRECTION_RIGHT:
		if (this->x + BLOCK_SIZE + this->speed <= GAME_WIDTH)
		{
			this->x += this->speed;
		}
		else
		{
			this->status = BULLET_STATUS_FREE;
		}
		break;
	}
}